<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Tamer_Model extends Model {

    public function setTable($master_table)
    {
   		$this->db_table = $master_table;
        return $this->db_table;
    }

    private function checkTable()
    {
        If( ! isset($this->db_table) ){ 
            redirect('dbview/pages/error_db', 'location'); 
        }else{
            return $this->db_table;
        }
    }

    public function init_db($id = FALSE)
    {
        if($id !== FALSE){
            $this->dbdata = $this->get_by_id($id);
        }else{
            $fields = $this->db->list_fields($this->db_table);
            foreach ($fields as $field) $this->dbdata[$field] = '';
        }
        return $this->dbdata;        
    }

    private function get_by_id($id, $table = FALSE) 
    {
        If( $table !== FALSE ){ $this->db_table = $table; }
        $this->checkTable();
        $return = array();
        $query = $this->db->query("SELECT * FROM `" .$this->db_table. "` WHERE id = '" .$id. "' LIMIT 1");
        
        if ($query->num_rows() > 0) {
            $this->dbdata = $query->row_array();
        }else{
            $this->dbdata = FALSE;
        }
        return $this->dbdata;
    }
    

    
    function findWhat($what_rules = NULL) {
        if($what_rules !== NULL){
            $this->what= " $what_rules ";
        }else{
            $this->what= ' * ';
        }
    }

    
    // *************  Start News Block ********************
    /**
    * @note	Random SideBar Block
    * @info	
    * @author	Dennis T. Kaplan
    * @access	public
    * @param	none 
    * @return	String
    */	
    public function randomBlock()
	{
        $sql = "SELECT `Title`, `Body` FROM `blocks` ORDER BY RAND() LIMIT 1";
        $query = $this->db->query($sql);
        $row = $query->row(); 
        $string = '<h3 class="SideBar">'.$row->Title.'</h3><p class="SideBar">'.$row->Body.'</p>';
        return $string;
    }

    
    /**
     * @note	newsTicker
     * @info	The News Ticker
     * @author	Dennis T. Kaplan
     * @access	private
     * @param	 
     * @return	String
     */	
    public function newsTicker()
    {
        $sql = "SELECT `id`, `Title` FROM `news` ORDER BY `id` DESC";
        $query = $this->db->query($sql);
        $rows = $query->result();        
        $i = '<ul id="news" class="SideBar" style="padding: 3px; ">'."\n";
        foreach($rows as $row){
            $i .= '<li class="SideBar">'.anchor('news/article/'.$row->id, $row->Title,array('class'=>'SideBar')).'</li>'."\n";
        }
        $i .= '</ul>'."\n";
        return $i;

    }

    
    /**
    * @note	Recent News
    * @info	
    * @author	Dennis T. Kaplan
    * @access	public
    * @param	none 
    * @return	String
    */	
    public function recentNews()
	{
		$this->load->helper('text');
        $this->load->library('pagination');
        
        $this->total = $this->db->count_all('news');
        $limit = 5;
        
        $this->db->orderby('id', 'desc');
        
		$query = $this->db->get('news', $limit);
        
		$list = '<h2 class="SideBar">Recent News</h2>';
		foreach($query->result() as $row){ 
			$list .= '<h3 class="SideBar">'.$row->Title.'</h3>';
			$list .= '<p class="SideBar">' . word_limiter($row->Body, 10) .'</p>';
			$list .= anchor('news/article/'.$row->id, 'View Article',array('class'=>'SideBar', 'title'=>$row->Title));
			$list .= '<hr class="SideBar" />';
		}
        
        $list .= $this->pagination->create_links();
        $list .= anchor('news/index','Historical Index', array('class'=>'SideBar'));
        return $list;
        
    }

    
    // *************  Start Contact info ********************
    
    public function get_uid() 
    {
        if( ! isset($this->uid) ) {
            $this->uid = $this->session->userdata('uid');
        }
        return $this->uid;	
    }
    
	public function get_People_id() {
        if( ! isset($this->pid)){
            $this->get_uid();
            $query = $this->db->query("SELECT `people_id` FROM `contact_user_people` WHERE user_id = '$this->uid' LIMIT 1");
            if ($query->num_rows() > 0) {
                $row = $query->row();
                $this->pid = $row->people_id;
            }else{
                return $this->pid = FALSE;
            }
        }
		return $this->pid;	
    }

	public function get_Org_id() {
        if( ! isset($this->oid ) ) {
            $this->get_uid();
            $query = $this->db->query('SELECT `org_id` FROM `contact_user_org` WHERE user_id = '.$this->uid.' LIMIT 1');
            if ($query->num_rows() > 0) {
                $row = $query->row();
                $this->oid = $row->org_id;
            }else{
                return $this->oid = FALSE;
            }
        }
        return $this->oid;
    }




    
    // *************  Start Places info ********************
    
    
    function countrySelect($selected = False)
	{

        $sql = "SELECT FIPS, Country FROM places_country";
		$query = $this->db->query($sql);
		$output = '<select name="Country">';

		foreach ($query->result() as $row)
		{
            $sel = ($selected != $row->FIPS) ? '' : ' selected="selected"';
            $output .= '<option value="' . $row->FIPS. '" $sel>' .$row->Country. '</option>';
		}
		$output .= '</select>'; 
		return $output;	
	}

    function postcodeSelect($selected = '')
	{
        $query = $this->Placedb->Postcode($selected);
        
		$output = '<select name="postcode">';
		foreach ($query->result() as $row) {
            $sel = ($selected != $row->code) ? '' : ' selected="selected"';
            $output .= '<option value="'.$row->code. '" $sel. >' . $row->code. '</option>';
		}
		$output .= '</select>'; 
		return $output;		
	}

	function Country($searchstring = False)
	{
        if($searchstring === False)
        {
            $sql = "SELECT FIPS, Country FROM places_country";
        }else{
            $sql = "SELECT FIPS, Country FROM places_country
                    WHERE Country LIKE '$searchstring%'";
        }
		return $this->db->query($sql);
	}
    
    
}
?>
